<?php
include(dirname(__FILE__).'/../../../includes.php5');
header('Content-Type: text/html; charset=UTF-8');

include_once($project_path.'/class/user/user.class.php5');
session_start();
include_once($project_path.'/class/fw/dictionary.class.php5');

userUser::checkSession();

$action=(isset($_GET['action'])?$_GET['action']:'');
switch($action)
{
	case "show":
		include_once($project_path.'/class/fw/smarty.class.php5');
		$smarty = new fwSmarty();
		$h = opendir($project_path.'/lang/');
		$langs = array();
		while($f = readdir($h))
		{
			if($f!='.' && $f!='..' && is_dir($project_path.'/lang/'.$f) && file_exists($project_path.'/lang/'.$f.'/index.php5'))
			{
				include($project_path.'/lang/'.$f.'/index.php5');
				$langs[$f] = $lang_info;
				if(!is_file($project_path.'/view/css/user/img/flag_'.$f.'.gif'))
				{
					if(isset($lang_info['flag']) && is_file($project_path.'/lang/'.$f.'/'.$lang_info['flag']))
					{
						copy($project_path.'/lang/'.$f.'/'.$lang_info['flag'], $project_path.'/view/css/user/img/flag_'.$f.'.gif');
					}
				}
			}
		}
		$smarty->assign('languages', $langs);
		
		echo 'register_popup.show("'.fwConverter::text2JSString($smarty->getContents('user/register.tpl')).'");
userPasswordMessages = new Array(\'«GLOBAL_PASSWORD_LEVEL_LOW»\', \'«GLOBAL_PASSWORD_LEVEL_NORMAL»\', \'«LOBAL_PASSWORD_LEVEL_HIGHT»\', \'«GLOBAL_PASSWORD_LEVEL_HIGHEST»\')
registerInit();';
		break;
	case "checkUser":
		include_once($project_path.'/class/user/auth.class.php5');
		if(!userAuth::isLoginCorrect(isset($_POST['login'])?$_POST['login']:''))
		{
			echo 'registerError("«USER_REGISTER_ERROR_CORRECT»");result_validity=0;';
		}
		elseif(!userAuth::isLoginFree(isset($_POST['login'])?$_POST['login']:''))
		{
			echo 'registerError("«USER_REGISTER_ERROR_FREE»");result_validity=0;';
		}
		else
		{
			echo 'result_validity=1;';
		}
		break;		
	case "save":
		include_once($project_path.'/class/user/auth.class.php5');
		if(!userAuth::isLoginCorrect(isset($_POST['txt_login'])?$_POST['txt_login']:''))
		{
			echo 'registerError("«USER_REGISTER_ERROR_CORRECT»", true);';
		}
		elseif(!userAuth::isLoginFree(isset($_POST['txt_login'])?$_POST['txt_login']:''))
		{
			echo 'registerError("«USER_REGISTER_ERROR_FREE»", true);';
		}
		elseif(!isset($_POST['txt_password']) || !isset($_POST['txt_confirm']) || $_POST['txt_password'] != $_POST['txt_confirm'])
		{
			echo 'registerError("«USER_REGISTER_ERROR_CONFIRM»", true);';
		}
		else
		{
			$ua = new userAuth();
			$ua->login = (isset($_POST['txt_login'])?$_POST['txt_login']:'');
			$ua->password = md5(isset($_POST['txt_password'])?$_POST['txt_password']:'');
			$ua->save();

			$u = userUser::getUser('tt', $ua->id, true, false);
			$u->displayLogin = (isset($_POST['txt_login'])?$_POST['txt_login']:'');
			$u->language = isset($_POST['txt_language'])?$_POST['txt_language']:'';
			if((!isset($_POST['txt_name']) || $_POST['txt_name']=='') && (!isset($_POST['txt_firstname']) || $_POST['txt_firstname'] == ''))
			{
				$u->person->name = isset($_POST['txt_login'])?$_POST['txt_login']:'';
			}
			else 
			{
				$u->person->name = (isset($_POST['txt_name'])?$_POST['txt_name']:'');
				$u->person->firstName = (isset($_POST['txt_firstname'])?$_POST['txt_firstname']:'');
			}
			$u->person->email = (isset($_POST['txt_email'])?$_POST['txt_email']:'');
			$u->person->save();
			$u->save();

			include_once($project_path.'/class/fw/login/loginTt.class.php5');
			list($responseOk, $login) = fwLoginTt::processLogin(array('login' => (isset($_POST['txt_login'])?$_POST['txt_login']:''), 'password' => md5(isset($_POST['txt_password'])?$_POST['txt_password']:'')));
			if($responseOk)
			{
				userUser::login('tt', $login, true, true, true);
				$_SESSION['current_user']->displayJS();
				echo 'register_popup.hide();
userConnectionRefreshLogin();';
			}
			else
			{
				echo 'registerError("«USER_REGISTER_ERROR_UNKNOW»");';
			}
			break;
		}
		break;
}
?>